package org.csu.backofficemanagersys.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Component
public class IsLoginInterceptor implements HandlerInterceptor  {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(request.getSession().getAttribute("loginAdmin") != null){
            return true;
        }else {
            response.sendRedirect(request.getContextPath()+"/loginForm");
        }
        return false;
        //默认值为false，代表不继续往后执行。如果为true，即使拦截了也依然会执行被拦截请求对应的controller层对应的方法。因此为true和没加拦截没区别
        //为false的话，被拦截的请求不会被执行它对应的controller方法
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}
